package com.abe.mvp.model;

import java.lang.reflect.Type;

import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import com.abe.config.HttpUtils;
import com.abe.mvp.contact.WebServerContact;
import com.abe.mvp.model.bean.http.ExerciseBean;
import com.abe.mvp.model.bean.http.HttpBean;
import com.abe.mvp.model.bean.http.ScanBean;
import com.abe.mvp.model.bean.http.UserBean;
import com.abe.mvp.model.bean.http.VersionBean;
import com.abe.utils.JsonHelper;
import com.abe.utils.NetUtils;
import com.abe.utils.NormalUtils;
import com.google.gson.reflect.TypeToken;

public class WebServerModelImp implements WebServerContact.Model {

	@Override
	public void getQRCode(final IWebServerBack callBack) {
		String URL = HttpUtils.GetUrl(HttpUtils.API_GET_QRCODE);
		RequestParams params = new RequestParams(URL);
		// params.addBodyParameter("mac", "c0:21:0d:41:66:31");
		params.addBodyParameter("mac", NetUtils.getLocalMacAddressFromWifiInfo());
		System.out.println(URL);
		x.http().post(params, new Callback.CommonCallback<String>() {

			@Override
			public void onCancelled(CancelledException exception) {

			}

			@Override
			public void onError(Throwable throwable, boolean arg1) {
				callBack.onResult(1, null, throwable.getMessage());
			}

			@Override
			public void onFinished() {
				callBack.onFinish();
			}

			@Override
			public void onSuccess(String result) {
				System.out.println(result);
				Type type = new TypeToken<HttpBean<ScanBean>>() {
				}.getType();
				HttpBean<ScanBean> bean = JsonHelper.parseObject(result, type);
				if (bean.getSuccess() == 1) {
					callBack.onResult(1, bean.getData(), "");
				} else {
					callBack.onResult(0, null, bean.getMessage());
				}
			}
		});
	}

	@Override
	public void getScanLoginUserInfo(String num, final IWebServerBack callBack) {
		String URL = HttpUtils.GetUrl(HttpUtils.API_SCAN_LOGIN);
		RequestParams params = new RequestParams(URL);
		params.addBodyParameter("mac",
				NetUtils.getLocalMacAddressFromWifiInfo());
		System.out.println(URL);
		x.http().post(params, new Callback.CommonCallback<String>() {

			@Override
			public void onCancelled(CancelledException exception) {

			}

			@Override
			public void onError(Throwable throwable, boolean arg1) {
				callBack.onResult(1, null, throwable.getMessage());
			}

			@Override
			public void onFinished() {
				callBack.onFinish();
			}

			@Override
			public void onSuccess(String result) {
				System.out.println(result);
				Type type = new TypeToken<HttpBean<UserBean>>() {
				}.getType();
				HttpBean<UserBean> bean = JsonHelper.parseObject(result, type);
				if (bean.getSuccess() == 1) {
					callBack.onResult(1, bean.getData(), "");
				} else {
					callBack.onResult(0, null, bean.getMessage());
				}
			}
		});
	}

	@Override
	public void getVersionInfo(final IWebServerBack callBack) {
		String URL = HttpUtils.GetUrl(HttpUtils.API_VERSION_UPDATE);
		RequestParams params = new RequestParams(URL);
		System.out.println(URL);
		x.http().post(params, new Callback.CommonCallback<String>() {

			@Override
			public void onCancelled(CancelledException exception) {

			}

			@Override
			public void onError(Throwable throwable, boolean arg1) {
				callBack.onResult(1, null, throwable.getMessage());
			}

			@Override
			public void onFinished() {
				callBack.onFinish();
			}

			@Override
			public void onSuccess(String result) {
				System.out.println(result);
				Type type = new TypeToken<HttpBean<VersionBean>>() {
				}.getType();
				HttpBean<VersionBean> bean = JsonHelper.parseObject(result,
						type);
				if (bean.getSuccess() == 1) {
					callBack.onResult(1, bean.getData(), "");
				} else {
					callBack.onResult(0, null, bean.getMessage());
				}
			}
		});
	}

	@Override
	public void uploadExerciseData(ExerciseBean bean,
			final IWebServerBack callBack) {
		String URL = HttpUtils.GetUrl(HttpUtils.API_UPLOAD_EXERCISE_DATA);
		RequestParams params = new RequestParams(URL);
		params.addBodyParameter("openid", bean.getOpenid());
		params.addBodyParameter("mac",
				NetUtils.getLocalMacAddressFromWifiInfo());
		params.addBodyParameter("device_type", "1");
		String trainningData = JsonHelper.toJson(bean.getTrainningData());
		params.addBodyParameter("trainningData", trainningData);
		NormalUtils.saveCrashInfo2File("123123json", params.toJSONString());
		NormalUtils.saveCrashInfo2File("123123", params.toString());
		x.http().post(params, new Callback.CommonCallback<String>() {

			@Override
			public void onCancelled(CancelledException exception) {

			}

			@Override
			public void onError(Throwable throwable, boolean arg1) {
				callBack.onResult(1, null, throwable.getMessage());
			}

			@Override
			public void onFinished() {
				callBack.onFinish();
			}

			@SuppressWarnings("rawtypes")
			@Override
			public void onSuccess(String result) {
				NormalUtils.saveCrashInfo2File("123123result", result);
				HttpBean bean = JsonHelper.parseObject(result, HttpBean.class);
				if (bean.getSuccess() == 1) {
					callBack.onResult(1, null, "");
				} else {
					callBack.onResult(0, null, bean.getMessage());
				}
			}
		});
	}

}
